package com.weetop.cfw.other

import android.app.ActivityManager
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.os.Process
import android.text.Html
import android.view.KeyEvent
import android.view.View
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
import com.blankj.utilcode.util.ActivityUtils
import com.weetop.cfw.R
import com.weetop.cfw.base.ui.activity.CommonBaseActivity
import kotlinx.android.synthetic.main.activity_rich_text_web_view.*


class RichTextWebViewActivity : CommonBaseActivity(), View.OnClickListener {

    companion object {
        private const val WEB_TITLE = "WEB_TITLE"
        private const val RICH_TEXT = "RICH_TEXT"
        private const val URL = "isurl"

        fun startRichTextWebViewActivity(context: Context, webTitle: String, richText: String) {
            val intent = Intent(context, RichTextWebViewActivity::class.java)
            intent.putExtra(WEB_TITLE, webTitle)
            intent.putExtra(RICH_TEXT, richText)
            ActivityUtils.startActivity(intent)
        }

        fun startRichTextWebViewActivity(
            context: Context,
            webTitle: String,
            richText: String,
            isurl: Boolean
        ) {
            val intent = Intent(context, RichTextWebViewActivity::class.java)
            intent.putExtra(WEB_TITLE, webTitle)
            intent.putExtra(RICH_TEXT, richText)
            intent.putExtra(URL, isurl)
            ActivityUtils.startActivity(intent)
        }
    }

    /**
     * 用于获取布局的ID
     */
    override fun getLayoutId() = R.layout.activity_rich_text_web_view

    /**
     * 初始化控件
     */
    override fun initView(savedInstanceState: Bundle?) {
        setViewsOnClickListener(this, imageCommonRichTextBack)

        initWebView()
    }

    /**
     * 设置数据的方法
     */
    override fun initData(savedInstanceState: Bundle?) {
        val richTextData = intent!!.getStringExtra(RICH_TEXT)
        val webTitle = intent!!.getStringExtra(WEB_TITLE)
        val isurl = intent!!.getBooleanExtra(URL, false)
        textCommonRichTextTitle.text = webTitle
        if (isurl) {
            richTextWebView.loadUrl(richTextData)
        } else {
            val settings = richTextWebView.settings
            settings.javaScriptEnabled = true //支持JS

            val js = "<script type=\"text/javascript\">" +
                    "var imgs = document.getElementsByTagName('img');" +  // 找到img标签
                    "for(var i = 0; i<imgs.length; i++){" +  // 逐个改变
                    "imgs[i].style.width = '100%';" +  // 宽度改为100%
                    "imgs[i].style.height = 'auto';" +
                    "}" +
                    "</script>"
            richTextWebView.loadData(Html.fromHtml(richTextData).toString()+  js, "text/html; charset=UTF-8", null)
//            richTextWebView.settings.textZoom = 200
//            richTextWebView.loadData(Html.fromHtml(richTextData).toString(), "text/html; charset=UTF-8", null)
        }
    }

    /**
     * 初始化WebView
     */
    private fun initWebView() {
        richTextWebView.webViewClient = object : WebViewClient() {
            override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
                return false// 返回false
            }
        }

        val webSettings = richTextWebView.settings



        webSettings.useWideViewPort = true
        webSettings.loadWithOverviewMode = true
        webSettings.layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN
        //扩大比例的缩放
        webSettings.javaScriptEnabled = true

//        // 让WebView能够执行javaScript
//        webSettings.javaScriptEnabled = true
//        // 让JavaScript可以自动打开windows
//        webSettings.javaScriptCanOpenWindowsAutomatically = true
//        // 设置缓存
//        webSettings.setAppCacheEnabled(true)
//        // 设置缓存路径
////        webSettings.setAppCachePath("")
//        // 支持缩放(适配到当前屏幕)
//        webSettings.setSupportZoom(true)
//        // 将图片调整到合适的大小
//        webSettings.useWideViewPort = true
//        // 支持内容重新布局,一共有四种方式
//        // 默认的是NARROW_COLUMNS
//        webSettings.layoutAlgorithm = WebSettings.LayoutAlgorithm.NARROW_COLUMNS
//        // 设置可以被显示的屏幕控制
//        webSettings.displayZoomControls = true
//        // 设置默认字体大小
//        webSettings.defaultFontSize = 12
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
        if (richTextWebView != null)
            if (keyCode == KeyEvent.KEYCODE_BACK) {
                if (richTextWebView.canGoBack()) {
                    //goBack()表示返回WebView的上一页面
                    richTextWebView.goBack()
                    return true
                } else {
                    finish()
                    return true
                }
            }
        return false
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.imageCommonRichTextBack -> {
                if (richTextWebView.canGoBack()) {
                    //goBack()表示返回WebView的上一页面
                    richTextWebView.goBack()
                } else {
                    finish()
                }
            }
        }
    }


}
